﻿using System;
using System.ComponentModel;
using System.Globalization;
using SG.Ling.AutoTranslator.Common;

namespace SG.Ling.AutoTranslator.Translation
{
    public partial class BatchTranslator : BatchTranslatorBase
    {

        public BatchTranslator()
            : this(null)
        {
            // empty
        }

        public BatchTranslator(IContainer container)
            : base(container)
        {
            InitializeComponent();

            Infrastructure.BeforeClose += Infrastructure_BeforeClose;
        }

        private void Infrastructure_BeforeClose(object sender, EventArgs e)
        {
            IsClosing = true;
            Dispose();
        }

        public bool TranslateAsync(string text, CultureInfo sourceLanguage, CultureInfo targetLanguage)
        {
            var detected = NLP.Detector.DetectLanguage(text);
            if (sourceLanguage != null && !sourceLanguage.IsNeutralCulture && !Equals(detected, sourceLanguage))
                return false;

            AddToDownloadQueue(new TranslationKey(text, sourceLanguage, targetLanguage));
            return true;
        }
        
    }
}
